<?php
 defined('_JEXEC') or die('Restricted access');
 switch( JRequest::getVar( 'task' )) {
	case 'tkbmh':
		tkbmonhoc();
		break;
	case 'tkblop':
		tkblop();
		break;
	case 'tkblop_detail':
		tkblop_detail();
		break;
	case 'tkbsv':
		tkbsinhvien();
		break;
	case 'tkbsv_detail':
		tkbsinhvien_detail();
		break;
	case 'ajaxnganh':
		callbacknganh();
		break;
	case 'ajaxlop':
		callbacklop();
		break;
	default:
		viewDefault();
		break;
}
 function tkbmonhoc(){
 ?>
 	<script language="javascript" type="text/javascript">
                jQuery.noConflict();
                jQuery(document).ready(function(){
					jQuery('#tkb_monhoc th').parent().addClass('table-heading');
					jQuery('#tkb_monhoc tr:not([th]):even').addClass('even');
					jQuery('#tkb_monhoc tr:not([th]):odd').addClass('odd');
        	});
     </script>
   <?php
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM #__thoikhoabieu tkb join #__monhoc mh on(tkb.f_mamh=mh.f_mamh) left join #__canbo  cb on(cb.f_manv=tkb.f_manv) order by tkb.f_mamh,tkb.f_manh,tkb.f_thu asc";
		$db->setQuery( $query, 0);
		
		echo "<div class='divtitle'><div class='header_hongbang'><div class='header_hongbang_row1'>Đại Học Quốc Tế Hồng Bàng";
		echo "<br/>Phòng Đào Tạo</div></div>";
		echo "</div>";
		echo "<div class='title_thoikhoabieu'><h1 class='contentheading_thoikhoabieu'>THỜI KHÓA BIỂU MÔN HỌC</h1>";
		echo "<div class='header_hongbang_row1'>Học Kỳ 2 - Năm Học 10 -11</div></div>";
		echo '<br/>';
		echo '<br/>';
		echo '<br/>';
		echo "<div class='div_content_monhoc'>";
		echo "<table id=tkb_monhoc>";
		echo "<tr><th>Mã Môn Học</th><th>Tên Môn Học</th><th>Nhóm</th><th>Thứ</th><th>Tiết Bắt Đầu</th><th>Số Tiết</th><th>Phòng</th><th>Địa Điểm</th><th>Cán Bộ Giảng Dạy</th></tr>";
		if($rows = $db->loadObjectList()) {
			foreach ($rows as $row)
			{
		
				// Encode text to HTML formatting
				$mamonhoc = htmlspecialchars($row->F_MAMH, ENT_QUOTES);
				$tenmonhoc = mb_convert_encoding($row->F_TENMHVN,'HTML-ENTITIES', 'UTF-8');
				$nhom = htmlspecialchars($row->F_MANH, ENT_QUOTES);
				$thu = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$tietbatdau = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$sotiet = htmlspecialchars($row->F_SOTIET, ENT_QUOTES);
				$phong = htmlspecialchars($row->F_TENPH, ENT_QUOTES);
				$canbodaotao = htmlspecialchars($row->F_MANV, ENT_QUOTES);
				echo "<tr align=center>";
				echo "<td align=left>". $mamonhoc ."</td>";
				echo "<td align=left>". $tenmonhoc ."</td>";
				echo "<td>". $nhom ."</td>";
				echo "<td>".  $thu ."</td>";
				echo "<td>".  $tietbatdau ."</td>";
				echo "<td>".  $sotiet ."</td>";
				echo "<td>".  $phong ."</td>";
				echo "<td></td>";
				echo "<td align=left>".  $canbodaotao ."</td>";
				
				
				// Put horizontal rules between entries
				echo "</tr>";
			}
		}
		echo "</table></div>";
	}
	function viewDefault(){
	?>
  
        <h1 class="componentheading">Thời Khóa Biểu</h1>
        <u>
        	<li><a href="index.php?option=com_thoikhoabieu&task=tkbmh"><b>Thời Khóa Biểu Theo Môn Học HK II-2011</b></a></li>
            <br/>
            <li><a href="index.php?option=com_thoikhoabieu&task=tkblop"><b>Thời Khóa Biểu Theo Lớp HK II-2011</b></a></li>
            <br/>
            <li><a href="index.php?option=com_thoikhoabieu&task=tkbsv"><b>Thời Khóa Biểu Theo Sinh Viên HK II-2011</b></a></li>
            <br/>
        </u>	
	
	<?php }
	function tkblop(){
	?>
      <script language="javascript" type="text/javascript">
	 
        window.addEvent( 'domready', function() {
         
                $('drop-down-he').addEvent( 'change', function() {
         				
						var valueHe= jQuery('#drop-down-he').val();
						jQuery('#drop-down-nganh').children().remove().end().append('<option selected value="0"> ---------------------------------</option>');
							jQuery('#drop-down-lop').children().remove().end().append('<option selected value="0"> ---------------------------------</option>');
                       // $('ajax-container').setHTML("Loading...");
                        var url = 'index.php?option=com_thoikhoabieu&task=ajaxnganh&format=raw&he='+valueHe;
                         new Ajax(url,{
                                method: 'post',
                                onComplete: function( response) {
                                var resp = Json.evaluate(response);
								jQuery.each(resp, function(val, text) { 
								 	jQuery('#drop-down-nganh').append(jQuery('<option></option>').val(val).html(text));
								});

                                }
                        }).request();
                });
				
			  $('drop-down-nganh').addEvent( 'change', function() {
         				
						var valueNganh= jQuery('#drop-down-nganh').val();
						var valueHe= jQuery('#drop-down-he').val();
						
						jQuery('#drop-down-lop').children().remove().end().append('<option selected value="0"> ---------------------------------</option>');
                       // $('ajax-container').setHTML("Loading...");
                        var url = 'index.php?option=com_thoikhoabieu&task=ajaxlop&format=raw&nganh='+valueNganh+'&he='+valueHe;
                         new Ajax(url,{
                                method: 'post',
                                onComplete: function( response) {
                                var resp = Json.evaluate(response);
								jQuery.each(resp, function(val, text) { 
								 	jQuery('#drop-down-lop').append(jQuery('<option></option>').val(val).html(text));
								});

                                }
                        }).request();
                });	
				
				
        });
		  
        
        </script>
    <?php
	 		echo '<h1 class="contentheading">Thời Khóa Biểu Theo Lớp</h1>';
			echo '<br/>';
			echo '<form id="tkb_lop" name="tkb_lopForm" method="post" action="index.php?option=com_thoikhoabieu&task=tkblop_detail">';
			$options = array();
			$options[] = JHTML::_( 'select.option', '0', '---------------------------------' );
			$options[] = JHTML::_( 'select.option', 'DH', 'Đại Học' );
			$options[] = JHTML::_( 'select.option', 'LT', 'Liên Thông' );
			$options[] = JHTML::_( 'select.option', 'CD', 'Cao Đẳng' );
			$options[] = JHTML::_( 'select.option', 'TC', 'Trung Cấp' );
			echo JText::_("Chọn Hệ:");
			echo JText::_("&nbsp;&nbsp;");
			echo JHTML::_( 'select.genericlist', $options, 'drop-down-he' );
			echo JText::_("Chọn Ngành:");
			echo JText::_("&nbsp;&nbsp;");
			$options_nganh = array();
			$options_nganh[] = JHTML::_( 'select.option', '0', '---------------------------------' );
			echo JHTML::_( 'select.genericlist', $options_nganh, 'drop-down-nganh');
			echo JText::_("Chọn Lớp:");
			echo JText::_("&nbsp;&nbsp;");
			$options_nganh = array();
			$options_nganh[] = JHTML::_( 'select.option', '0', '---------------------------------' );
			echo JHTML::_( 'select.genericlist', $options_nganh, 'drop-down-lop' );
			echo JText::_("&nbsp;&nbsp;");
            echo '<input type="submit" name="Submit" class="button" value="Xem" />';
        	echo '</form>';
	}
	function callbacknganh(){
		$db =& JFactory::getDBO();
		$he=JRequest::getVar('he');
		$query = "SELECT * FROM #__lop where F_MAHE='".$he."'";
		$db->setQuery( $query, 0);
		$listnganh = array();
		if($rows = $db->loadObjectList()) {
			foreach ($rows as $row)
			{
				// Encode text to HTML formatting
				$manganh = htmlspecialchars($row->F_MANGANH, ENT_QUOTES);
				$nganh = htmlspecialchars($row->F_NGANH, ENT_QUOTES);
				$listnganh[$manganh] =$nganh;
			}
		}
		echo json_encode($listnganh);
	}
	function callbacklop(){
		$db =& JFactory::getDBO();
		$nganh=JRequest::getVar('nganh');
		$he=JRequest::getVar('he');
		$query = "SELECT * FROM #__lop where F_MANGANH='".$nganh."' and F_MAHE='".$he."'";
		$db->setQuery( $query, 0);
		$listlop = array();
		if($rows = $db->loadObjectList()) {
			foreach ($rows as $row)
			{
				// Encode text to HTML formatting
				$malop = htmlspecialchars($row->F_MALP, ENT_QUOTES);
				$tenlop = htmlspecialchars($row->F_MALP, ENT_QUOTES);
				$listlop[$malop] =$tenlop;
			}
		}
		echo json_encode($listlop);
	}
	function tkblop_detail(){
	?>
	<script language="javascript" type="text/javascript">
                jQuery.noConflict();
                jQuery(document).ready(function(){
					jQuery('th').parent().addClass('table-heading');
					jQuery('tr:not([th]):even').addClass('even');
					jQuery('tr:not([th]):odd').addClass('odd');
                    
                    
        	});
     </script>
     <?php
		$db =& JFactory::getDBO();
		$he=JRequest::getVar('drop-down-he');
		$nganh=JRequest::getVar('drop-down-nganh');
		$lop=JRequest::getVar('drop-down-lop');
		$query = "select * from jos_thoikhoabieu tkb join(select DISTINCT  f_mamh,f_manh from jos_lop join jos_lop_nhom_mh on(jos_lop.f_malp=jos_lop_nhom_mh.f_malp) where jos_lop.f_malp='".$lop."') temp on (tkb.f_mamh=temp.f_mamh and tkb.f_manh=temp.f_manh) join jos_monhoc mh on(tkb.f_mamh=mh.f_mamh) left join jos_canbo cb on(cb.f_manv=tkb.f_manv)";
		$db->setQuery( $query, 0);
		$listlop = array();
		echo "<h1 class='contentheading'>Thời Khóa Biểu Lớp :'".$lop."'</h1>";
		echo '<br/>';
		echo "<table id=tkb_lop_detail>";
		echo "<tr><th>Mã Môn Học</th><th>Tên Môn Học</th><th>Nhóm</th><th>Thứ</th><th>Tiết Bắt Đầu</th><th>Số Tiết</th><th>Phòng</th><th>Cán Bộ Giảng Dạy</th></tr>";
		if($rows = $db->loadObjectList()) {
			foreach ($rows as $row)
			{
					// Encode text to HTML formatting
				$mamonhoc = htmlspecialchars($row->F_MAMH, ENT_QUOTES);
				$tenmonhoc = mb_convert_encoding($row->F_TENMHVN,'HTML-ENTITIES', 'UTF-8');
				$nhom = htmlspecialchars($row->F_MANH, ENT_QUOTES);
				$thu = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$tietbatdau = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$sotiet = htmlspecialchars($row->F_SOTIET, ENT_QUOTES);
				$phong = htmlspecialchars($row->F_TENPH, ENT_QUOTES);
				$canbodaotao = htmlspecialchars($row->F_MANV, ENT_QUOTES);
				echo "<tr align=center>";
				echo "<td align=left>". $mamonhoc ."</td>";
				echo "<td align=left>". $tenmonhoc ."</td>";
				echo "<td>". $nhom ."</td>";
				echo "<td>".  $thu ."</td>";
				echo "<td>".  $tietbatdau ."</td>";
				echo "<td>".  $sotiet ."</td>";
				echo "<td>".  $phong ."</td>";
				echo "<td align=left>".  $canbodaotao ."</td>";
				
				// Put horizontal rules between entries
				echo "</tr>";
			}
		}
		echo "</table>";
	}
	
function tkbsinhvien(){
?>
	 		<h1 class="componentheading">Thời Khóa Biểu Của Sinh Viên</h1>
			<form id="tkb" name="thoikhoabieuForm" method="post" action="index.php?option=com_thoikhoabieu&task=tkbsv_detail">
				<label>Mã Sinh Viên: </label><input name="masv" type="text" id="masv" />
				<input type="submit" name="Submit" class="button" value="Xem" />
			</form>
            
<?php
}	
function tkbsinhvien_detail(){
		$db =& JFactory::getDBO();
		$masv=JRequest::getVar('masv');
		$query = "select * from (select diem.f_manh,diem.f_mamh from jos_sinhvien sv join jos_diem diem on(sv.f_masv=diem.f_masv) where sv.f_masv='".$masv."') sinhvien join jos_thoikhoabieu tkb on (sinhvien.f_manh=tkb.f_manh and sinhvien.f_mamh=tkb.f_mamh) order by tkb.f_thu asc";
		$db->setQuery( $query, 0);
		echo "<h1 class='contentheading'>Thời Khóa Biểu Của Sinh Viên</h1>";
		echo '<br/>';
		echo "<table id=tkb_sinhvien>";
		echo "<tr><th>Mã Môn Học</th><th>Tên Môn Học</th><th>Nhóm</th><th>Thứ</th><th>Tiết Bắt Đầu</th><th>Số Tiết</th><th>Phòng</th><th>Cán Bộ Giảng Dạy</th></tr>";
		if($rows = $db->loadObjectList()) {
			foreach ($rows as $row)
			{
		
				// Encode text to HTML formatting
				$mamonhoc = htmlspecialchars($row->F_MAMH, ENT_QUOTES);
				$tenmonhoc = mb_convert_encoding($row->F_TENMHVN,'HTML-ENTITIES', 'UTF-8');
				$nhom = htmlspecialchars($row->F_MANH, ENT_QUOTES);
				$thu = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$tietbatdau = htmlspecialchars($row->F_THU, ENT_QUOTES);
				$sotiet = htmlspecialchars($row->F_SOTIET, ENT_QUOTES);
				$phong = htmlspecialchars($row->F_TENPH, ENT_QUOTES);
				$canbodaotao = htmlspecialchars($row->F_MANV, ENT_QUOTES);
				echo "<tr align=center>";
				echo "<td align=left>". $mamonhoc ."</td>";
				echo "<td align=left>". $tenmonhoc ."</td>";
				echo "<td>". $nhom ."</td>";
				echo "<td>".  $thu ."</td>";
				echo "<td>".  $tietbatdau ."</td>";
				echo "<td>".  $sotiet ."</td>";
				echo "<td>".  $phong ."</td>";
				echo "<td align=left>".  $canbodaotao ."</td>";
				
				// Put horizontal rules between entries
				echo "</tr>";
			}
		}
		echo "</table>";
}
?>
	
